//
//  PasswordResetCell.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/26.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "PasswordResetCell.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

@interface PasswordResetCell() <UITextFieldDelegate>
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UITextField *txfPwd;
@property (nonatomic, strong) UIButton *btnShowStatus;
@end

@implementation PasswordResetCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.accessoryType = UITableViewCellAccessoryNone;
        [self addSubviews];
    }
    return self;
}

- (BOOL)willDealloc {
    if (_txfPwd) {
        [_txfPwd removeFromSuperview];
        _txfPwd = nil;
    }
    return YES;
}

- (void)addSubviews
{
    [self addSubview:self.lblTitle];
    self.lblTitle.centerY = self.height/2;
    
    // 添加密码控件
    [self addSubview:self.txfPwd];
    self.txfPwd.left = self.lblTitle.right;
    self.txfPwd.centerY = self.lblTitle.centerY;
    
    // 添加密码状态设置控件
    [self addSubview:self.btnShowStatus];
    self.btnShowStatus.centerY = self.txfPwd.centerY;
    self.btnShowStatus.left = self.txfPwd.right + 10.f;
    
    self.type = PasswordShowType_Ciphertext;
}

- (UILabel *)lblTitle
{
    if (!_lblTitle) {
        _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(Cell_X, 0, 85.f, 22.f)];
        _lblTitle.textAlignment = NSTextAlignmentLeft;
        _lblTitle.font = SystemFont(16.f);
    }
    return _lblTitle;
}

- (UITextField *)txfPwd
{
    if (!_txfPwd) {
        _txfPwd = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, Cell_W-60.f-85.f, 22.f)];
        _txfPwd.delegate = self;
        _txfPwd.placeholder = self.strPlaceholder;
        _txfPwd.clearButtonMode =UITextFieldViewModeWhileEditing;
        _txfPwd.autocorrectionType = UITextAutocorrectionTypeNo;
        _txfPwd.keyboardType = UIKeyboardTypeASCIICapable;
        _txfPwd.font = SystemFont(16.f);
        [_txfPwd setValue:SystemFont(16.f) forKeyPath:@"_placeholderLabel.font"];
        // 设置删除按钮
        _txfPwd.rightView = [self clearButtonClick];
        _txfPwd.rightViewMode = UITextFieldViewModeWhileEditing;
    }
    return _txfPwd;
}

- (UIButton *)btnShowStatus
{
    if (!_btnShowStatus) {
        _btnShowStatus = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.f, 40.f)];
        [_btnShowStatus addTarget:self action:@selector(btnIsShowPwdClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnShowStatus;
}

- (UIButton *)clearButtonClick
{
    UIButton *rightView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50.f, 50.f)];
    [rightView setImage:[UIImage imageNamed:@"login_clear"] forState:UIControlStateNormal];
    [rightView setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20.f)];
    [rightView addTarget:self action:@selector(clearEditClick:) forControlEvents:UIControlEventTouchUpInside];
    return rightView;
}

// 点击密码样式切换
- (void)btnIsShowPwdClick:(id)sender {
    
    // 密码显示模式互换
    self.txfPwd.enabled = NO; // 解决明文/密文切换导致光标错误的问题
    self.txfPwd.secureTextEntry = !self.txfPwd.secureTextEntry;
    self.txfPwd.enabled = YES;
    
    // 设置按钮图案切换
    NSString *strImageName = self.txfPwd.secureTextEntry?@"password_hidden":@"password_display";
    [self.btnShowStatus setImage:[UIImage imageNamed:strImageName] forState:UIControlStateNormal];
    [self.btnShowStatus setImage:[UIImage imageNamed:strImageName] forState:UIControlStateHighlighted];
    
    [self.txfPwd becomeFirstResponder];
}

- (void)clearEditClick:(id)sender
{
    self.txfPwd.text = @"";
    if(self.btnClearBlock) self.btnClearBlock();
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    // 限定输入位数
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.txfBecomeBlock) {
        self.txfBecomeBlock(self.tag, toBeString);
    }
    
    // 解决明文、密文切换时密码被清空的问题
    if (textField.isSecureTextEntry ) {
        textField.text = toBeString;
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.txfPwd resignFirstResponder];
    return YES;
}

#pragma mark - 对外接口：获取密码的值
- (NSString *)getPassword
{
    return self.txfPwd.text;
}

#pragma mark - 设置文字的颜色
- (void)setTextColor:(UIColor *)textColor
{
    self.lblTitle.textColor = textColor;
    [self.txfPwd setValue:[textColor colorWithAlphaComponent:0.5] forKeyPath:@"_placeholderLabel.textColor"];
    self.txfPwd.textColor = textColor;
}

#pragma mark - 设置密码样式
- (void)setType:(PasswordShowType)type
{
    self.txfPwd.secureTextEntry = (type == PasswordShowType_Ciphertext);
    if (self.txfPwd.secureTextEntry) {
        [_btnShowStatus setImage:[UIImage imageNamed:@"password_hidden"] forState:UIControlStateNormal];
        [_btnShowStatus setImage:[UIImage imageNamed:@"password_hidden"] forState:UIControlStateHighlighted];
    }
    else
    {
        [_btnShowStatus setImage:[UIImage imageNamed:@"password_display"] forState:UIControlStateNormal];
        [_btnShowStatus setImage:[UIImage imageNamed:@"password_display"] forState:UIControlStateHighlighted];
    }
}

- (void)setStrTitle:(NSString *)title
{
    self.lblTitle.text = title;
}

- (void)setStrPlaceholder:(NSString *)placeholder
{
    self.txfPwd.placeholder = placeholder;
}

@end
